package com.Arsanesia.TempleRush.Models;

import java.util.HashMap;
import java.util.Map;

import com.Arsanesia.TempleRush.Enumeration.GeenieEnumeration;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.Json.Serializable;
import com.badlogic.gdx.utils.OrderedMap;

public class Profile implements Serializable {

	private int highscore;
	private int templebuilt;
	private int totalscore;
	private int totaltemple;
	private int currentscore;
	private int currenttemple;
	//geenie achievements
	private HashMap<String, Integer> geenieAchievements;
	private int tapcount;
	
	public Profile(){
		highscore = 0;
		templebuilt = 0;
		totalscore = 0;
		totaltemple = 0;
		currentscore =0;
		currenttemple = 0;
		geenieAchievements = new HashMap<String, Integer>();
		for(GeenieEnumeration key : GeenieEnumeration.values()){
			if(key.ordinal()< GeenieEnumeration.WHITE.ordinal() || key.equals(GeenieEnumeration.GOLD)){
				geenieAchievements.put(key.name(), 0);
			}
		}
		tapcount = 0;
	}
	public int getHighScore(){
		return highscore;
	}
	public int getTempleBuilt(){
		return templebuilt;
	}
	public int getTotalScore(){
		return totalscore;
	}
	public int getTotalTemple(){
		return totaltemple;
	}
	public int getCurrentScore(){
		return currentscore;
	}
	public int getCurrentTemple(){
		return currenttemple;
	}
	public int getTappedGeenieTypeCount(GeenieEnumeration ge){
		return geenieAchievements.get(ge.name());
	}
	public boolean isGeenieAchievementCompleted(String ge){
//		return geenieAchievements.get(ge)>=1000;
		return geenieAchievements.get(ge)>=1;
	}
	public boolean isTapAchievementCompleted(int imgindex){
//		return tapcount>=(Math.pow(10, imgindex)*1000);
		return tapcount>=(Math.pow(10, imgindex)*1);
	}
	public boolean isTempleAchievementCompleted(int imgindex){
//		return totaltemple>=((Math.pow(10, imgindex))*1000);
		return totaltemple>=((Math.pow(10, imgindex))*1);
	}
	public void resetProfile(){
		currentscore = 0;
		currenttemple = 0;
		totalscore = 0;
		totaltemple = 0;
		highscore = 0;
		templebuilt = 0;
		geenieAchievements = new HashMap<String, Integer>();
		for(GeenieEnumeration key : GeenieEnumeration.values()){
			if(key.ordinal()< GeenieEnumeration.WHITE.ordinal() || key.equals(GeenieEnumeration.GOLD)){
				geenieAchievements.put(key.name(), 0);
			}
		}
	}
	public void updateProfile(int score, int built, int tapcount){
		currentscore = score;
		currenttemple = built;
		
		totalscore+=score;
		totaltemple+=built;
		
		this.tapcount += tapcount;
		changeHighScore(score,built);
	}
	public void updateTappedGeenieType(int[] tappedgeenie)
	{
		for(String key: geenieAchievements.keySet()){
			GeenieEnumeration temptype = GeenieEnumeration.valueOf(key);
			//increment with the current tapped geenie 
			int countGeenie =geenieAchievements.get(key) + tappedgeenie[temptype.ordinal()];
			//update the hashmap
			geenieAchievements.put(key, countGeenie);
		}
	}
	private void changeHighScore(int score, int built){
		if(score>highscore || (built > templebuilt && highscore == score))
		{
			//store highscore if score bigger or score same with templebuilt larger
			highscore = score;
			templebuilt = built;
		}

	}
	@Override
	public void write(Json json) {
		json.writeValue("highscore", highscore);
		json.writeValue("templebuilt", templebuilt);
		json.writeValue("totalscore", totalscore);
		json.writeValue("totaltemple", totaltemple);
		json.writeValue("tapcount",tapcount);
		json.writeValue("geenieAchievements", geenieAchievements);
		
	}

	@Override
	public void read(Json json, OrderedMap<String, Object> jsonData) {
		highscore = json.readValue("highscore",Integer.class, jsonData);
		templebuilt = json.readValue("templebuilt", Integer.class, jsonData);
		totalscore = json.readValue("totalscore",Integer.class, jsonData);
		totaltemple = json.readValue("totaltemple", Integer.class, jsonData);
		tapcount = json.readValue("tapcount", Integer.class, jsonData);
		HashMap<String,String> geenies = json.readValue("geenieAchievements", HashMap.class, String.class,  jsonData);
		for(String key: geenies.keySet()){
			String temp = geenies.get(key);
			geenieAchievements.put(key, Integer.valueOf(temp));
		}
	}
	
}
